package cn.weiwei.WHadoop.hdfs.filesystem;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by weiwei02 on 2017/2/18.
 */
public class NumCount {
    /**最终数据集合*/
    private static final Map<Integer,Integer> integerMap = new HashMap<Integer, Integer>();
    int a = 0;
    private class CountThread extends Thread{
        private Integer[] integers;
        Map<Integer,Integer> integerMap2 = new HashMap<Integer, Integer>();
        CountThread(Integer[] integers){
            super();
            this.integers = integers;

        }
        @Override
        public void run() {
            //执行数据分类 单个线程计算数字总数
            for (int i =0; i < integers.length; i++){
                //判断integerMap2中是否已包含这个数字
                if (integerMap2.containsKey(integers[i])){
                    integerMap2.replace(integers[i],integerMap2.get(integers[i])+1);
                }else {
                    integerMap2.put(integers[i],1);
                }
            }
            copy();
            a++;
        }
        //执行数据复制  相当于化简操作
        public synchronized void copy(){
            Set<Integer> integerSet = integerMap2.keySet();
            for (Integer integer : integerSet){
                    if (integerMap.containsKey(integer)){
                        integerMap.replace(integer,integerMap2.get(integer) + integerMap.get(integer));
                    }else {
                        integerMap.put(integer,integerMap2.get(integer));
                    }
            }
        }

    }

    public static void main(String[] args) throws InterruptedException {
        long time = System.currentTimeMillis();
        Integer[] integer1 = {1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,31,31,21,21,3,3,3,3,4,4,4,4,4,4,5,5,6,6,7,8,8,9,9,9,9,9,9};
        Integer[] integer2 = {1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,31,31,21,21,3,3,3,3,4,4,4,4,4,4,5,5,6,6,7,8,8,9,9,9,9,9,9,10,10,10,10,1};
        NumCount numCount = new NumCount();
        CountThread countThread1 = numCount.new CountThread(integer1);
        CountThread countThread2 = numCount.new CountThread(integer2);
        countThread1.start();
        countThread2.start();
        while (numCount.a != 2){
        }MMMMM
        //遍历输出最终结果
        Set<Integer> integerSet = integerMap.keySet();
        f9or (Integer integer : integerSet){
            System.out.print(integer);
            System.out.print( " : ");
            System.out.println(integerMap.get(integer));
        }
        System.out.print(System.currentTimeMillis() - time);
    }

}
